<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>列表初始化 (C++11 起)</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/list_initialization","wgTitle":"cpp/language/list initialization","wgCurRevisionId":64825,"wgArticleId":2727,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/list_initialization","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_list_initialization skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">列表初始化 <span class="t-mark-rev t-since-cxx11">(C++11 起)</span></h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="initialization.html" title="cpp/language/initialization">初始化</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"><a href="initialization.html" title="cpp/language/initialization">初始化器</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="default_initialization.html" title="cpp/language/default initialization">默认初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="value_initialization.html" title="cpp/language/value initialization">值初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="direct_initialization.html" title="cpp/language/direct initialization">直接初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="copy_initialization.html" title="cpp/language/copy initialization">复制初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><strong class="selflink">列表初始化</strong></td></tr>
<tr class="t-nv"><td colspan="5"><a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">聚合初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="reference_initialization.html" title="cpp/language/reference initialization">引用初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="copy_elision.html" title="cpp/language/copy elision">复制消除</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"><a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">静态初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="zero_initialization.html" title="cpp/language/zero initialization">零初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="constant_initialization.html" title="cpp/language/constant initialization">常量初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"><a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">动态非局部初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">有序动态初始化</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">无序动态初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5">类成员初始化</td></tr>
<tr class="t-nv"><td colspan="5"><a href="constructor.html" title="cpp/language/initializer list">成员初始化器列表</a></td></tr>
<tr class="t-nv"><td colspan="5"><a href="data_members.html#.E6.88.90.E5.91.98.E5.88.9D.E5.A7.8B.E5.8C.96" title="cpp/language/data members">类内<i>花括号或等号</i>初始化器</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>从花括号初始化器列表初始化对象
</p>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95">语法</span></h3>
<h4><span class="mw-headline" id=".E7.9B.B4.E6.8E.A5.E5.88.97.E8.A1.A8.E5.88.9D.E5.A7.8B.E5.8C.96">直接列表初始化</span></h4>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">T</span> <span class="t-spar">object</span> <code><b>{ </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>};</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">T</span> <code><b>{ </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>}</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>new</b></code> <span class="t-spar">T</span> <code><b>{ </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>}</b></code>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">Class</span> <code><b>{</b></code> <span class="t-spar">T</span> <span class="t-spar">member</span> <code><b>{ </b></code><span class="t-spar">arg1, arg2, ...</span> <code><b>}; };</b></code>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">Class</span><code><b>::</b></code><span class="t-spar">Class</span><code><b>() : </b></code><span class="t-spar">member</span><code><b>{</b></code><span class="t-spar">arg1, arg2, ...</span><code><b>} {...</b></code>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<h4><span class="mw-headline" id=".E5.A4.8D.E5.88.B6.E5.88.97.E8.A1.A8.E5.88.9D.E5.A7.8B.E5.8C.96">复制列表初始化</span></h4>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">T</span> <span class="t-spar">object</span> <code><b>= {</b></code><span class="t-spar">arg1, arg2, ...</span><code><b>};</b></code>
</td>
<td> (6)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">function</span><code><b>( { </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>} )</b></code>
</td>
<td> (7)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>return { </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>} ;</b></code>
</td>
<td> (8)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">object</span><code><b>[ { </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>} ]</b></code>
</td>
<td> (9)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">object</span> <code><b>= </b></code> <code><b>{ </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>}</b></code>
</td>
<td> (10)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">U</span><code><b>( { </b></code> <span class="t-spar">arg1, arg2, ...</span> <code><b>} )</b></code>
</td>
<td> (11)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">Class</span> <code><b>{</b></code> <span class="t-spar">T</span> <span class="t-spar">member</span> <code><b>=</b></code> <code><b>{ </b></code><span class="t-spar">arg1, arg2, ...</span> <code><b>}; };</b></code>
</td>
<td> (12)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>在下列情形进行列表初始化：
</p>
<ul><li> 直接列表初始化（考虑 explicit 和非 explicit 构造函数）
</li></ul>
<div class="t-li1"><span class="t-li">1)</span> 以<i>花括号初始化器列表</i>（即花括号环绕的可以为空的表达式或花括号初始化器列表的列表）初始化具名变量</div>
<div class="t-li1"><span class="t-li">2)</span> 以<i>花括号初始化器列表</i>初始化无名临时量</div>
<div class="t-li1"><span class="t-li">3)</span> 以 <a href="new.html" title="cpp/language/new">new 表达式</a>初始化具有动态存储期的对象，其初始化器是花括号初始化器列表</div>
<div class="t-li1"><span class="t-li">4)</span> 在不使用等号的非静态<a href="data_members.html#.E6.88.90.E5.91.98.E5.88.9D.E5.A7.8B.E5.8C.96" title="cpp/language/data members">数据成员初始化器</a>中</div>
<div class="t-li1"><span class="t-li">5)</span> 在构造函数的<a href="constructor.html" title="cpp/language/initializer list">成员初始化列表</a>中，若使用<i>花括号初始化器列表</i></div>
<ul><li> 复制列表初始化（考虑 explicit 和非 explicit 构造函数，但只调用非 explicit 构造函数）
</li></ul>
<div class="t-li1"><span class="t-li">6)</span> 以等号后的<i>花括号初始化器列表</i>初始化具名变量</div>
<div class="t-li1"><span class="t-li">7)</span> 函数调用表达式中，以<i>花括号初始化器列表</i>为实参，以列表初始化对函数形参初始化</div>
<div class="t-li1"><span class="t-li">8)</span> 在以花括号初始化器列表为返回表达式的 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">return</span></span></span> 语句中，以列表初始化对返回的对象初始化</div>
<div class="t-li1"><span class="t-li">9)</span> 在具有用户定义的 <code>operator[]</code> 的<a href="operator_member_access.html#.E5.86.85.E5.BB.BA.E7.9A.84.E4.B8.8B.E6.A0.87.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/operator member access">下标表达式</a>中，以列表初始化对重载运算符的形参初始化</div>
<div class="t-li1"><span class="t-li">10)</span> 在<a href="operator_assignment.html" title="cpp/language/operator assignment">赋值表达式</a>中，以列表初始化对重载的运算符的形参初始化</div>
<div class="t-li1"><span class="t-li">11)</span> <a href="explicit_cast.html" title="cpp/language/explicit cast">函数式转型表达式</a>或其他构造函数调用，其中<i>花括号初始化器列表</i>用作构造函数实参。以复制初始化对构造函数的形参初始化（注意：此例中的类型 U 不是被列表初始化的类型；但 U 的构造函数的形参是）</div>
<div class="t-li1"><span class="t-li">12)</span> 在使用等号的非静态<a href="data_members.html#.E6.88.90.E5.91.98.E5.88.9D.E5.A7.8B.E5.8C.96" title="cpp/language/data members">数据成员初始化器</a>中</div>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p><code>T</code> 类型的对象的列表初始化的效果是：
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<ul><li> 若 <code>T</code> 是聚合类，且初始化器列表拥有单个（可以为 cv 限定的）相同类型或派生类型的元素，则从该元素初始化对象（对于复制列表初始化为<a href="copy_initialization.html" title="cpp/language/copy initialization">复制初始化</a>，对于直接列表初始化为<a href="direct_initialization.html" title="cpp/language/direct initialization">直接初始化</a>）。
</li><li> 否则，若 <code>T</code> 是字符数组且初始化器列表拥有单个元素，是类型适当的字符串字面量，则照常<a href="aggregate_initialization.html#.E5.AD.97.E7.AC.A6.E6.95.B0.E7.BB.84" title="cpp/language/aggregate initialization">从字符串字面量初始化</a>数组。
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<ul><li> 若花括号初始化器列表为空，且 <code>T</code> 是拥有默认构造函数的类类型，则进行<a href="value_initialization.html" title="cpp/language/value initialization">值初始化</a>。
</li></ul>
<ul><li> 否则，若 <code>T</code> 是<a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">聚合类型</a>，则进行<a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">聚合初始化</a>。
</li></ul>
</td>
<td><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></td></tr>
<tr class="t-rev t-since-cxx14"><td>
<ul><li> 若 <code>T</code> 是<a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">聚合类型</a>，则进行<a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">聚合初始化</a>。
</li></ul>
<ul><li> 否则，若花括号初始化器列表为空，且 <code>T</code> 是拥有默认构造函数的类类型，则进行<a href="value_initialization.html" title="cpp/language/value initialization">值初始化</a>。
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
<ul><li> 否则，若 <code>T</code> 是 <span class="t-lc"><a href="../utility/initializer_list.html" title="cpp/utility/initializer list">std::initializer_list</a></span> 的特化，则依据其语境，以花括号初始化器列表<span class="t-rev-inl t-until-cxx17"><span>所初始化的同类型的纯右值</span> <span><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></span></span>直接初始化或复制初始化该 <code>T</code> 对象。
</li></ul>
<ul><li> 否则，以两个阶段考虑 <code>T</code> 的构造函数：
</li></ul>
<dl><dd><ul><li> 检验所有接受 <span class="t-lc"><a href="../utility/initializer_list.html" title="cpp/utility/initializer list">std::initializer_list</a></span> 作为其唯一实参，或若剩余实参都具有默认值则为其首个实参的构造函数，并通过<a href="overload_resolution.html" title="cpp/language/overload resolution">重载决议</a>与单个 <span class="t-lc"><a href="../utility/initializer_list.html" title="cpp/utility/initializer list">std::initializer_list</a></span> 类型的实参进行匹配。
</li></ul>
</dd></dl>
<dl><dd><ul><li> 若前一阶段未产生匹配，则 <code>T</code> 的所有构造函数都参与针对由花括号初始化器列表的各元素所组成的实参集的<a href="overload_resolution.html#.E5.88.97.E8.A1.A8.E5.88.9D.E5.A7.8B.E5.8C.96.E4.B8.AD.E7.9A.84.E9.9A.90.E5.BC.8F.E8.BD.AC.E6.8D.A2.E5.BA.8F.E5.88.97" title="cpp/language/overload resolution">重载决议</a>，其受到只允许非窄化转换的限制。若此阶段为复制列表初始化产生了 explicit 构造函数为最佳匹配，则编译失败（注意：简单复制初始化中，完全不考虑 explicit 构造函数）。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<ul><li> 否则，若 <code>T</code> 是拥有固定底层类型的有作用域或无作用域<a href="enum.html" title="cpp/language/enum">枚举类型</a>，且若花括号初始化器列表仅拥有一个初始化器，且若从该初始化器到底层类型的转换不是窄化，且若初始化是直接列表初始化，则以将初始化器转换到其底层类型的结果初始化该枚举。
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<ul><li> 否则（若 <code>T</code> 不是类类型），若花括号初始化器列表仅拥有一个元素，且 <code>T</code> 要么不是引用类型，要么是引用类型而其所引用的类型与该元素的类型相同或为其基类，则<a href="direct_initialization.html" title="cpp/language/direct initialization">直接初始化</a>（于直接列表初始化）或<a href="copy_initialization.html" title="cpp/language/copy initialization">复制初始化</a>（于复制列表初始化）这个 <code>T</code>，但不允许窄化转换。
</li></ul>
<ul><li>否则，若 <code>T</code> 是引用类型，则列表初始化一个被引用类型<span class="t-rev-inl t-since-cxx20"><span>或其对应的已知边界数组类型</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>的临时量，并将引用绑定到该临时量。（若引用是非 const 左值引用则失败）
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<dl><dd> 若被引用类型是未知边界数组类型 <span class="t-c"><span class="mw-geshi cpp source-cpp">U<span class="br0">[</span><span class="br0">]</span></span></span> ，则临时量类型为 <span class="t-c"><span class="mw-geshi cpp source-cpp">U<span class="br0">[</span>N<span class="br0">]</span></span></span> ，如同它定义为 <span class="t-c"><span class="mw-geshi cpp source-cpp">U temp<span class="br0">[</span><span class="br0">]</span><span class="coMULTI">/*init-list*/</span><span class="sy4">;</span></span></span> ，其中 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="coMULTI">/*init-list*/</span></span></span> 是初始化器列表。
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<ul><li> 否则，若花括号初始化器列表无任何元素，则<a href="value_initialization.html" title="cpp/language/value initialization">值初始化</a> <code>T</code>。
</li></ul>
<h3><span class="mw-headline" id=".E7.AA.84.E5.8C.96.E8.BD.AC.E6.8D.A2">窄化转换</span></h3>
<p>列表初始化通过禁止下列转换，对<a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a>加以限制：
</p>
<ul><li> 从浮点类型到整数类型的转换
</li></ul>
<ul><li> 从 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">long</span> <span class="kw4">double</span></span></span> 到 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span> 或 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">float</span></span></span> 的转换，以及从 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span> 到 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">float</span></span></span> 的转换，除非源是<a href="constant_expression.html" title="cpp/language/constant expression">常量表达式</a>且不发生溢出
</li></ul>
<ul><li> 从整数类型到浮点类型的转换，除非源是其值能完全存储于目标类型的常量表达式
</li></ul>
<ul><li> 从整数或无作用域枚举类型到不能表示原类型所有值的整数类型的转换，除非源是其值能完全存储于目标类型的常量表达式
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> 从指针或成员指针类型到 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span> 的转换
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p>每个初始化器子句均<a href="eval_order.html" title="cpp/language/eval order">按顺序早于</a>花括号初始化器列表中后随于它的任何初始化器子句。这与<a href="operator_other.html#.E5.86.85.E5.BB.BA.E7.9A.84.E5.87.BD.E6.95.B0.E8.B0.83.E7.94.A8.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/operator other">函数调用表达式</a>相反，它是<a href="eval_order.html" title="cpp/language/eval order">无顺序的</a>。
</p><p>花括号初始化器列表不是表达式，从而没有类型，如 <span class="t-c"><span class="mw-geshi cpp source-cpp">decltype<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">}</span><span class="br0">)</span></span></span> 非良构。没有类型意味着模板类型推导无法推导出与花括号初始化器列表相匹配的类型，因此给定声明 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> <span class="kw4">void</span> f<span class="br0">(</span>T<span class="br0">)</span><span class="sy4">;</span></span></span> 则表达式 <span class="t-lc">f({1,2,3})</span> 非良构。然而，模板形参可以另行推导，如 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v<span class="br0">(</span><a href="../iterator/istream_iterator.html"><span class="kw668">std::<span class="me2">istream_iterator</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="br0">(</span><a href="../io/cin.html"><span class="kw1756">std::<span class="me2">cin</span></span></a><span class="br0">)</span>, <span class="br0">{</span><span class="br0">}</span><span class="br0">)</span></span></span> 的情况，其中迭代器类型从首个实参推导，但亦用于第二形参位置。对于<a href="template_argument_deduction.html#.E5.85.B6.E4.BB.96.E8.AF.AD.E5.A2.83" title="cpp/language/template argument deduction">使用关键词 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">auto</span></span></span> 的类型推导</a>中有一个例外，在复制列表初始化中将任何花括号初始化器列表均推导为 <span class="t-lc"><a href="../utility/initializer_list.html" title="cpp/utility/initializer list">std::initializer_list</a></span>。
</p><p>亦因为花括号初始化器列表没有类型，所以在将它用作重载函数调用的实参时，适用<a href="overload_resolution.html#.E5.88.97.E8.A1.A8.E5.88.9D.E5.A7.8B.E5.8C.96.E9.87.8C.E7.9A.84.E9.9A.90.E5.BC.8F.E8.BD.AC.E6.8D.A2.E5.BA.8F.E5.88.97" title="cpp/language/overload resolution">重载决议的特殊规则</a>。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>聚合体直接从同类型的单元素花括号初始化器列表进行复制/移动初始化，但非聚合体首先考虑 initializer_list 构造函数：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> X <span class="br0">{</span>
    X<span class="br0">(</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    X<span class="br0">(</span><span class="kw4">const</span> X<span class="sy3">&amp;</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">struct</span> Q <span class="br0">{</span>
    Q<span class="br0">(</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    Q<span class="br0">(</span>Q <span class="kw4">const</span><span class="sy3">&amp;</span><span class="br0">)</span> <span class="sy1">=</span> <span class="kw1">default</span><span class="sy4">;</span>
    Q<span class="br0">(</span><a href="../utility/initializer_list.html"><span class="kw960">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span>Q<span class="sy1">&gt;</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
  X x<span class="sy4">;</span>
  X x2 <span class="sy1">=</span> X <span class="br0">{</span> x <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 复制构造函数（非聚合初始化）</span>
  Q q<span class="sy4">;</span>
  Q q2 <span class="sy1">=</span> Q <span class="br0">{</span> q <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 初始化器列表构造函数（非复制构造函数）</span>
<span class="br0">}</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="co2">#include &lt;vector&gt;</span>
<span class="co2">#include &lt;map&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
 
<span class="kw1">struct</span> Foo <span class="br0">{</span>
    <a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> mem <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 非静态成员的列表初始化</span>
    <a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> mem2<span class="sy4">;</span>
    Foo<span class="br0">(</span><span class="br0">)</span> <span class="sy4">:</span> mem2<span class="br0">{</span><span class="sy2">-</span><span class="nu0">1</span>, <span class="sy2">-</span><span class="nu0">2</span>, <span class="sy2">-</span><span class="nu0">3</span><span class="br0">}</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// 构造函数中的成员列表初始化</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<a href="../utility/pair.html"><span class="kw1096">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span><a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a>, <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span> f<span class="br0">(</span><a href="../utility/pair.html"><span class="kw1096">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span><a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a>, <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span> p<span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw1">return</span> <span class="br0">{</span>p.<span class="me1">second</span>, p.<span class="me1">first</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// return 语句中的列表初始化</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">int</span> n0<span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>     <span class="co1">// 值初始化（为零）</span>
    <span class="kw4">int</span> n1<span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span>    <span class="co1">// 直接列表初始化</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s1<span class="br0">{</span><span class="st0">'a'</span>, <span class="st0">'b'</span>, <span class="st0">'c'</span>, <span class="st0">'d'</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// initializer_list 构造函数调用</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s2<span class="br0">{</span>s1, <span class="nu0">2</span>, <span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span>           <span class="co1">// 常规构造函数调用</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> s3<span class="br0">{</span><span class="nu12">0x61</span>, <span class="st0">'a'</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// initializer_list 构造函数偏好 (int, char)</span>
 
    <span class="kw4">int</span> n2 <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 复制列表初始化</span>
    <span class="kw4">double</span> d <span class="sy1">=</span> <span class="kw4">double</span><span class="br0">{</span><span class="nu16">1.2</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 临时量的列表初始化，然后复制初始化</span>
 
    <a href="../container/map.html"><span class="kw1281">std::<span class="me2">map</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span> m <span class="sy1">=</span> <span class="br0">{</span> <span class="co1">// 嵌套列表初始化</span>
           <span class="br0">{</span><span class="nu0">1</span>, <span class="st0">"a"</span><span class="br0">}</span>,
           <span class="br0">{</span><span class="nu0">2</span>, <span class="br0">{</span><span class="st0">'a'</span>, <span class="st0">'b'</span>, <span class="st0">'c'</span><span class="br0">}</span> <span class="br0">}</span>,
           <span class="br0">{</span><span class="nu0">3</span>, s1<span class="br0">}</span>
    <span class="br0">}</span><span class="sy4">;</span>
 
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> f<span class="br0">(</span><span class="br0">{</span><span class="st0">"hello"</span>, <span class="st0">"world"</span><span class="br0">}</span><span class="br0">)</span>.<span class="me1">first</span> <span class="co1">// 函数调用中的列表初始化</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 
    <span class="kw4">const</span> <span class="kw4">int</span> <span class="br0">(</span><span class="sy3">&amp;</span>ar<span class="br0">)</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 绑定左值引用到临时数组</span>
    <span class="kw4">int</span><span class="sy3">&amp;&amp;</span> r1 <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 绑定右值引用到临时 int</span>
<span class="co1">//  int&amp; r2 = {2}; // 错误：不能绑定右值到非 const 左值引用</span>
 
<span class="co1">//  int bad{1.0}; // 错误：窄化转换</span>
    <span class="kw4">unsigned</span> <span class="kw4">char</span> uc1<span class="br0">{</span><span class="nu0">10</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 可以</span>
<span class="co1">//  unsigned char uc2{-1}; // 错误：窄化转换</span>
 
    Foo f<span class="sy4">;</span>
 
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> n0 <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> n1 <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> n2 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> s1 <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> s2 <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> s3 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="kw1">for</span><span class="br0">(</span><span class="kw4">auto</span> p<span class="sy4">:</span> m<span class="br0">)</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> p.<span class="me1">first</span> <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> p.<span class="me1">second</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="kw1">for</span><span class="br0">(</span><span class="kw4">auto</span> n<span class="sy4">:</span> f.<span class="me1">mem</span><span class="br0">)</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span><span class="sy4">;</span>
    <span class="kw1">for</span><span class="br0">(</span><span class="kw4">auto</span> n<span class="sy4">:</span> f.<span class="me1">mem2</span><span class="br0">)</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">world
0 1 1
abcd cd aa
1 a
2 abc
3 abcd
1 2 3 -1 -2 -3</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1467">CWG 1467</a>
</td>
<td> C++14
</td>
<td> 禁止同类型的聚合体和 char 数组初始化
</td>
<td> 允许同类型初始化
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1467">CWG 1467</a>
</td>
<td> C++14
</td>
<td> 单元素列表的 std::initializer_list 构造函数优先级高于复制构造函数
</td>
<td> 单元素列表直接初始化
</td></tr></table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<ul><li> <a href="default_initialization.html" title="cpp/language/default initialization">默认初始化</a>
</li><li> <a href="direct_initialization.html" title="cpp/language/direct initialization">直接初始化</a>
</li><li> <a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">聚合初始化</a>
</li><li> <a href="copy_initialization.html" title="cpp/language/copy initialization">复制初始化</a>
</li></ul>

<!-- 
NewPP limit report
Preprocessor visited node count: 3406/1000000
Preprocessor generated node count: 8603/1000000
Post‐expand include size: 59010/2097152 bytes
Template argument size: 22310/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:2727-0!*!0!!zh!*!zh!* and timestamp 20200223064054 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/list_initialization&amp;oldid=64825">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/list_initialization&amp;oldid=64825</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/list_initialization">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年2月16日 (星期日) 21:33。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过24,365次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.912 secs. -->
	</body>
<!-- Cached 20200223064055 -->
</html>